<?php
namespace app\admin\controller;
use think\Controller;
class Admin extends Base
{
   public function index()
    {    
        return $this->fetch();
    }  

    public function login()
    {   
        return $this->fetch();
    }  
    
    public function loginsave()
    {    
          $data = input('post.');
          //后台管理系统登录校验
          $validate = validate('Admin');
          if(!$validate->scene('add')->check($data)) {
              $this->error($validate->getError());
            }
         //判断是否post传送
         if (request()->isPost()) {
	         	 //验证码验证
	         	 if(!captcha_check($data['code'])){
	               $this->error('验证码错误');
	            };
	            //判断该管理员账号是否存在
	             $res=model('Superuser')->get(['username'=>$data['username']]);
	             if (!$res) {
	                 $this->error('该账号无权限，请重新登录');  
	              } 
	              //判断管理员密码是否正确
	             if ($res->password!=$data['password']) {
	             	 $this->error('密码错误，请重新输入');
	             }
	             //记住账号密码
	             if (isset($data['baomi'])) {
	             	 cookie('id',$res->id,3600);
	             	 cookie('username',$res->username,3600);
	             	 cookie('password',$data['password'],3600);
	             }else{
	             	cookie('username',null);
	             	cookie('password',null);
	             }
               return $this->success('登录成功',url('Admin/index'));
    	 }
    }  //---loginsave

   public function nav(){
     	 return $this->fetch();
     }

     //从蔬菜表和供应商表获取蔬菜信息以及对应的供应商
     public function vegetables(){
          $vegetables = model('Vegetables')->getvegetables();
          $this->assign('vegetables',$vegetables);
       	 return $this->fetch();
     }

     //蔬菜商品详情页查询蔬菜商品,以及对应的供应商
     public function shucaichaxun(){
        if(request()->isPost()){
             $data=input('post.');
            // dump($data['cate_name']);exit();
             $shucaichaxun = model('Vegetables')->shucaichaxun($data['cate_name']);
            // dump($shucaichaxun);exit();
           	 $this->assign('shucaichaxun',$shucaichaxun);
             return $this->fetch();
         }
     }

     //删除一条蔬菜数据
     public function shucaidelete(){
        $data=input();
     // dump($data['id']);exit();
       $res = model('Vegetables')->where('vid',$data['id'])->update(['zhuangtai'=>-1]);
         if($res){
          $this->success('删除成功','admin/vegetables');
        }else{
          $this->error('删除失败');
        }
     }

    //编辑一条蔬菜信息
    public function shucaiedit(){
         $data=input();
       // print_r($data['id']);exit();
        $vegetable = model('Vegetables')->getvegetable($data['id']);
       //dump($vegetable);exit();
        $this->assign('vegetable',$vegetable);
         return $this->fetch();
    }

    //编辑一条信息后保存
     public function shucaieditsave()
     {
        $data=input('post.');
      //  print_r($data['gid']);exit();
      // print_r($data['gname']);exit();
      //$gid = gid($data['gid']);
      // print_r($gid);exit();
      //   $leixing = shucaileixing($data['leixing']);
      //   $id=$data['vid'];
      //   $zhuangtai=$data['zhuangtai'];
      //   $status=$data['status'];
        $res=model('Vegetables')->save($_POST, [$gid = $data['gid']],[$leixing = shucaileixing($data['leixing'])],[$id=$data['vid']],[$zhuangtai=$data['zhuangtai']],[$status=$data['status']]);
       if($res){
          $this->success('修改成功');
        }else{
          $this->error('修改失败');
        }
     }
    
    //添加一条蔬菜信息
     public function shucaiadd(){
       return $this->fetch();
     }


     public function jieshao(){
        return $this->fetch();
     }


// public function addBnaner(){
//     if(input('isAjax') == 1){ //异步上传图片
//       // 获取表单上传文件
//       $file = request()->file('file'); //layui默认的文件name 即为 file
//       if(empty($file)){
//         return json(['info'=>'请选择上传文件！','status'=>0]);
//       }
//       // 移动到框架应用根目录/public/upload/ 目录下，并修改文件名为时间戳
//       $info = $file->move(ROOT_PATH.'public'.DS.'upload'.DS.'image'.DS,time());
//             //下面两行即为多图上传的文件名重定义
//             //$filename = time().rand(10,100); //时间戳+随机数
//             //$info = $file->move(ROOT_PATH.'public'.DS.'upload'.DS.'article'.DS,$filename);
//       if($info){      
//     return json(['info'=>$info->getSaveName(),'status'=>1]); //文件名称
//       }else{
//     return json(['info'=>'文件上传失败啦！','status'=>0]);
//       }
//     }
// }
// 
// 
// 
// public function doUploadPic()//上传模块
//     {
//         $upload = new \Think\Upload();
//         $upload->maxSize = 3145728;
//         $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
//         $upload->rootPath = './Public/'; // 设置附件上传根目录
//         $upload->savePath = 'upload/'; // 设置附件上传子目录
//         $info = $upload->upload();
//         if(!$info){
//             $this->error($upload->getError());
//         }else{
//             foreach($info as $file){
//         $data = '/Public'.$file['savepath'] . $file['savename'];
//         $file_a=$data;
//         echo '{"code":0,"msg":"成功上传","data":{"src":"'.$file_a.'"}}';
//       }
//         }
// }
}
